package com.link.base.base.saleactivity.service;

import com.link.base.base.saleactivity.model.SaleActivity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: 汪大伟
 * @date: 2018/6/1
 * 定时更新活动
 */

@Component
public class AtyScheduledTasks {

    @Resource
    private SaleActivityService saleActivityService;


    public void activityTasks() throws Exception {
        SaleActivity saleActivity = new SaleActivity();
        saleActivity.setPageFlag(false);
        List<SaleActivity> salList = saleActivityService.queryByExamplePage(saleActivity);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss ");
        String date = sdf.format(new Date());
        int updateNum = 0;
        for (SaleActivity entity : salList) {
            if (entity.getActivityEndDate() != null && entity.getActivityStartDate() != null) {
                if (entity.getActivityEndDate().compareTo(date) < 0 && "InProgress".equals(entity.getActivityState())) {
                    entity.setActivityState("Completed");
                    updateNum++;
                    entity.setRow_status("UPDATE");
                    saleActivityService.upsert(entity);
                } else if (entity.getActivityStartDate().compareTo(date) < 0
                        && entity.getActivityEndDate().compareTo(date) > 0
                        && "New".equals(entity.getActivityState())) {
                    entity.setActivityState("InProgress");
                    updateNum++;
                    entity.setRow_status("UPDATE");
                    saleActivityService.upsert(entity);
                } else {
                    continue;
                }
            }
            {
                continue;
            }
        }
    }
}
